﻿using System;
using System.Collections.Generic;
using System.Drawing;
using ProgramMain.Database;
using ProgramMain.Layers;

namespace ProgramMain.NetVis
{
    public class FibreRange : RangeBase
    {

        public FibreRange(Layers.VisLayer vis)
            : base(vis)
        { }

        private const int _step = 4;
        public static int Step
        { get { return _step; } }

        public const int BaseWidth = 11;
         public const int BaseHeight = 35;

        public override Size VirtualSize()
        {
            return new Size(BaseWidth, BaseHeight);
        }

       
        public int Number { get; set; }

        public override int? ObjectID()
        {
            int? res = null;
            if (DobleFibreInVertex)
            {
                if (ToFibreID.HasValue)
                {
                    if (!Row.IsToFibre_IDNull() && !Row.IsVertex1_IDNull() && Row.ToFibre_ID == ToFibreID.Value)
                        res = Row.Vertex1_ID;
                    if (!Row.IsToFibre2_IDNull() && !Row.IsVertex2_IDNull() && Row.ToFibre2_ID == ToFibreID.Value)
                        res = Row.Vertex2_ID;
                }
            }
            else
            {
                if (!Row.IsVertex1_IDNull())
                {
                    res = Row.Vertex1_ID;
                }
                else if (!Row.IsVertex2_IDNull())
                {
                    res = Row.Vertex2_ID;
                }
            }

            return res;
        }
      
        public int Enter = 1;
        public bool DobleFibreInVertex = false;
        public int CurveElementKindID { get; set; }

        /// <summary>
        /// Возвращает или задает ИД элемета, которому принадлежит волокно.
        /// </summary>
        public int CurveElementID { get; set; }

        private void LoadHint()
        {

            if (!Row.IsHINT1Null())
                Hint = Row.HINT1;

            if (!Row.IsHINT2Null())
                Hint = string.IsNullOrEmpty(Hint) ? Row.HINT2 : Hint + "\n" + Row.HINT2;

            if (string.IsNullOrEmpty(Hint)) Hint = "Волокно не разварено";

        }
        private int? _toFibreID;
        public int? ToFibreID
        {
            get { return _toFibreID; }
            set { _toFibreID = value; }
        }

        public Point VirtualConnectionPointIn { get; set; }
        public Point VirtualConnectionPointOut { get; set; }

        public Point ConnectionPointIn
        {
            get { return Vis.GetLocation(VirtualConnectionPointIn); }
        }
        public Point ConnectionPointOut
        {
            get { return Vis.GetLocation(VirtualConnectionPointOut); }
        }


        public string Caption { get; set; }
      
        private SimpleOpticNetDB.FibreRow _row;
        public SimpleOpticNetDB.FibreRow Row
        {
            get { return _row; }
            set
            {
                if (value != null)
                {
                    _row = value;
                    Caption = Row.Number.ToString();
                    FibreID = _row.ID;
                }
            }
        }

        private int _fontSize
        { get { return (Vis.ZoomCcale - 10) / 2 + 7; } }



        public event EventHandler<ShiftConnection> ShiftFibreEvent;

        public override void CalcCoordinate(Point vPoint)
        {
            Point inOld = VirtualConnectionPointIn;
            Point outOld = VirtualConnectionPointOut;
            if (Enter == 1)
            {
                VirtualPoint = new Point(vPoint.X + Number*(BaseWidth + Step), vPoint.Y + BodyRange.BaseHeight);
                VirtualConnectionPointOut = new Point(VirtualPoint.X + BaseWidth/2, VirtualPoint.Y + BaseHeight);
                VirtualConnectionPointIn = new Point(VirtualPoint.X + BaseWidth/2, VirtualPoint.Y);
            }

            if (Enter == 2)
            {
                VirtualPoint = new Point(vPoint.X + Number*(BaseWidth + Step), vPoint.Y - BaseHeight);
                VirtualConnectionPointIn = new Point(VirtualPoint.X + BaseWidth/2, VirtualPoint.Y + BaseHeight);
                VirtualConnectionPointOut = new Point(VirtualPoint.X + BaseWidth/2, VirtualPoint.Y);
            }

            if (ShiftFibreEvent != null)
            {
               ShiftFibreEvent(this, new ShiftConnection(outOld, VirtualConnectionPointOut));
               if (Enter == 1) ShiftFibreEvent(this, new ShiftConnection(inOld, VirtualConnectionPointIn));
            }
        }

        public override void Draw(System.Drawing.Graphics layer)
        {
            if (!Visible) return;
            if (Status == RangeStatus.Free || Status == RangeStatus.Connection)
            Status = ToFibreID.HasValue ? RangeStatus.Connection : RangeStatus.Free;
            LoadHint();
            base.Draw(layer);
            if (!string.IsNullOrEmpty(Caption) && _fontSize > 4)
            {
                Font font = new Font("Arial", _fontSize, FontStyle.Regular);
                Brush brushCaption = FillRange ? Brushes.White : Brushes.Black;
                StringFormat sf = new StringFormat(StringFormatFlags.DirectionVertical);
                layer.DrawString(Caption, font, brushCaption, this.Location + new Size(0, 0), sf);
                //layer.DrawString()
            }

        }

       public override RangeStatus Status
        {
            get
            {
                return base.Status;

            }
            set
            {
                _status = value;

                switch (_status)
                {
                    case RangeStatus.Active:
                        ColorRange = Brushes.Green;
                        FillRange = true;
                        break;
                    case RangeStatus.Connection:
                        ColorRange = Brushes.Gray;
                        FillRange = true;
                        break;
                    case RangeStatus.CreateConnection:
                        ColorRange = Brushes.Red;
                        FillRange = true;
                        break;
                    case RangeStatus.Free:
                         ColorRange = Brushes.White;
                        FillRange = false;
                        break;
                  default:
                        ColorRange = Brushes.White;
                        FillRange = false;
                        break;
                }
               
            }
        }

       public bool Find(int fibre_id, int PlanState)
        {
           if(fibre_id == 37543)
           {}
            if (PlanState == 1)
                return (!Row.IsLinkCableFibreIDNull() && Row.LinkCableFibreID == fibre_id);
            return (!Row.IsToFibre_IDNull() && Row.ToFibre_ID == fibre_id) || (!Row.IsToFibre2_IDNull() && Row.ToFibre2_ID == fibre_id);
        }
    }
}